(() => {
    var timer = null
    var qrKey = null
    let closeWin = document.querySelector('#closeWin')

    // 关闭登录窗口
    closeWin.onclick = () => window.loginApi.closeWinLogin()

    // 生成二维码
    axios.get(`http://139.196.192.50:3000/login/qr/key?timeStamp=${Date.now()}`).then(({ data }) => {
        qrKey = data.data.unikey
        axios.get(`http://139.196.192.50:3000/login/qr/create?key=${data.data.unikey}&timeStamp=${Date.now()}`).then(({ data }) => {
            new QRCode(document.querySelector('#QR'), {
                text: `${data.data.qrurl}`,
                width: 220,
                height: 220,
                colorDark: "#000000",
                colorLight: "#ffffff",
                correctLevel: QRCode.CorrectLevel.H
            });
            // 轮询检查二维码状态
            timer = setInterval(() => {
                axios.get(`http://139.196.192.50:3000/login/qr/check?key=${qrKey}&timeStamp=${Date.now()}`).then(({data}) => {
                    console.log(data);
                    if (data.code === 800) {
                        console.log('二维码已过期, 请重新获取')
                        return clearInterval(timer)
                    }else if (data.code === 803) {
                        console.log('授权登录成功')
                        console.log(data);
                        window.loginApi.receiveCookie(data.cookie)
                        window.loginApi.closeWinLogin()
                        return clearInterval(timer)
                    }
                })
            }, 10000)
        })
    })
})()